<?php
class Model_Pager
{
    var $max_row;
    var $max_page;
    var $page_id;
    var $info;
    
    public function __construct()
    {
        $this->max_row  = 5;
        $this->max_page = 5;
        $this->page_id  = 1;
        $this->info     = array();
    }
    public function getPagerInfo($count_all, $p = 1, $max = '', $max_page = '')
    {
        $max      = ($max == '' ? $this->max_row : $max);
        $max_page = ($max_page == '' ? $this->max_page : $max_page);
        
        $all_page = ceil( $count_all / $max );
        if( $all_page == 1 ){
            $this->info = array(
                    "all"        => $count_all,
                    "start"      => 1,
                    "end"        => $count_all
            );
            return $this->info;
        }
        elseif( $all_page > 1 ){
            if( isset( $p ) && is_numeric( $p ) ){
                $page = $p;
                if( $page > $all_page ){
                    $page = 1;
                }
            }
            else{
                $page = 1;
            }
            $start = ( $page - 1 ) * $max;
            $end   = ( ( $start + $max ) > $count_all ? $count_all : ( $start + $max ) );
            $next  = ( ( $end + $max ) > $count_all ? ( $count_all - $end ) : $max );
            if( $all_page <= $max_page ){
                $page_start = 1;
                $page_end   = $all_page;
            }
            else{
                if( $page < floor( $max_page / 2 ) ){
                    $page_start = 1;
                    $page_end   = $max_page;
                }
                elseif( $page > ( $all_page - floor( $max_page / 2 ) ) ){
                    $page_start = $all_page - $max_page + 1;
                    $page_end   = $all_page;
                }
                else{
                    $page_start = ( ( $page - floor( $max_page / 2 ) ) > 0 ? ( $page - floor( $max_page / 2 ) ) : 1 );
                    $page_end   = $page_start + $max_page - 1;
                }
            }
            $this->info = array(
    
                    "all"        => $count_all,
                    "start"      => $start + 1,
                    "end"        => $end,
                    "prev"       => $max,
                    "next"       => $next,
                    "page"       => $page,
                    "all_page"   => $all_page,
                    "page_start" => $page_start,
                    "page_end"   => $page_end,
                    "page_next"  => ( $page < $all_page ? $page + 1 : "" ),
                    "page_prev"  => ( $page > 1 ? $page - 1 : "" )
            );
            return $this->info;
        }
        else{
            $this->info = array();
            return false;
        }
    }
}
?>